package com.example.demo.controller;

import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.pojo.Houser;
import com.example.demo.service.IHouserService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Houser")
public class HouserController {

	@Autowired
	private IHouserService HouserService;

	@RequestMapping("/selectHouser")
	public void selectHouser(HttpServletResponse response, Houser Houser, String openid) throws IOException {
		List<Houser> list = HouserService.selectHouser();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/selectHouserByOpenid")
	public void selectHouserByOpenid(HttpServletResponse response, Houser Houser, String openid) throws IOException {
		Houser = HouserService.selectHouserByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(Houser);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/addHouser")
	public void addHouser(HttpServletResponse response, Houser Houser, String openid,String pwd,String name,String sex,Integer age,String tel,String preference,String email,String healthy,String community,String logo) throws IOException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("注册");
		Houser.setAge(age);
		Houser.setName(name);
		Houser.setOpenid(openid);
		Houser.setPwd(pwd);
		Houser.setSex(sex);
		Houser.setTel(tel);
		Houser.setPreference(preference);
		Houser.setEmail(email);
		Houser.setHealthy(healthy);
		Houser.setLogo("http://localhost:8080/images/"+logo);
		Houser.setTime(df.format(new Date()));
		HouserService.insertSelective(Houser);
		Gson gson = new Gson();
		String json = gson.toJson(HouserService.selectHouserByLimitOne());
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/updateHouser")
	public void updateHouser(HttpServletResponse response, Houser Houser, String openid,String pwd,String name,String sex,Integer age,String tel,Integer id,String preference,String email,String healthy,String logo,Integer
							  isimg) throws IOException {
		Houser.setAge(age);
		Houser.setName(name);
		Houser.setPwd(pwd);
		Houser.setSex(sex);
		Houser.setTel(tel);
		Houser.setId(id);
		Houser.setPreference(preference);
		Houser.setEmail(email);
		Houser.setHealthy(healthy);
		if(isimg==1){
			Houser.setLogo("http://localhost:8080/images/"+logo);
		}
		HouserService.updateByPrimaryKeySelective(Houser);
		Gson gson = new Gson();
		String json = gson.toJson(HouserService.selectByPrimaryKey(id));
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}


	@RequestMapping("/updateHouserByLogo")
	public void updateHouser(HttpServletResponse response, Houser Houser, String logo,Integer id) throws IOException {
		Houser.setLogo("http://localhost:8080/images/"+logo);
		Houser.setId(id);
		HouserService.updateByPrimaryKeySelective(Houser);
		Gson gson = new Gson();
		String json = gson.toJson(HouserService.selectByPrimaryKey(id));
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/HouserLogin")
	public void HouserLogin(HttpServletResponse response, Houser Houser, String tel,String pwd) throws IOException {
		System.out.println("tel:"+tel);
		Houser = HouserService.HouserLogin(tel, pwd);
		Gson gson = new Gson();
		String json = gson.toJson(Houser);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

}
